<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="风险对象" prop="riskObject">
        <el-input
          v-model="queryParams.riskObject"
          placeholder="请输入风险对象"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="风险单元" prop="riskUnit">
        <el-input
          v-model="queryParams.riskUnit"
          placeholder="请输入风险单元"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="风险事件" prop="riskEvent">
        <el-input
          v-model="queryParams.riskEvent"
          placeholder="请输入风险事件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管控级别" prop="controlLevel">
        <el-input
          v-model="queryParams.controlLevel"
          placeholder="请输入管控级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管控方式" prop="controlMethod">
        <el-input
          v-model="queryParams.controlMethod"
          placeholder="请输入管控方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 管控分类措施1：一级下拉 -->
      <el-form-item label="管控分类措施1" prop="controlCategoryMeasure1">
        <el-select 
          v-model="queryParams.controlCategoryMeasure1"
          placeholder="请选择管控分类措施1"
          clearable
          @change="handleQueryMeasure1Change">
          <el-option
            v-for="item in controlCategoryMeasure1Options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- 管控分类措施2：二级级联下拉 -->
      <el-form-item label="管控分类措施2" prop="controlCategoryMeasure2">
        <el-select
          v-model="queryParams.controlCategoryMeasure2"
          placeholder="请选择管控分类措施2"
          clearable
          :disabled="!queryParams.controlCategoryMeasure1">
          <el-option
            v-for="item in controlCategoryMeasure2Options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="管控分类措施3" prop="controlCategoryMeasure3">
        <el-input
          v-model="queryParams.controlCategoryMeasure3"
          placeholder="请输入管控分类措施3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:measures:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:measures:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:measures:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:measures:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="measuresList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="风险对象" align="center" prop="riskObject" />
      <el-table-column label="风险单元" align="center" prop="riskUnit" />
      <el-table-column label="风险事件" align="center" prop="riskEvent" />
      <el-table-column label="管控级别" align="center" prop="controlLevel" />
      <el-table-column label="管控方式" align="center" prop="controlMethod" />
      <el-table-column label="管控分类措施1" align="center" prop="controlCategoryMeasure1" />
      <el-table-column label="管控分类措施2" align="center" prop="controlCategoryMeasure2" />
      <el-table-column label="管控分类措施3" align="center" prop="controlCategoryMeasure3" />
      <el-table-column label="管控措施描述" align="center" prop="controlMeasureDescription" />
      <el-table-column label="隐患排查内容" align="center" prop="hiddenDangerInspectionContent" />
      <el-table-column label="隐患检查标准" align="center" prop="hiddenDangerInspectionStandard" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:measures:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:measures:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改风险管理措施对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="风险对象" prop="riskObject">
              <el-input v-model="form.riskObject" placeholder="请输入风险对象" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="风险单元" prop="riskUnit">
              <el-input v-model="form.riskUnit" placeholder="请输入风险单元" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="风险事件" prop="riskEvent">
              <el-input v-model="form.riskEvent" placeholder="请输入风险事件" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管控级别" prop="controlLevel">
              <el-input v-model="form.controlLevel" placeholder="请输入管控级别" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="管控方式" prop="controlMethod">
              <el-input v-model="form.controlMethod" placeholder="请选择风险对象" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管控措施分类1" prop="controlCategoryMeasure1">
              <el-select 
                v-model="form.controlCategoryMeasure1"
                placeholder="请选择管控措施分类1"
                @change="handleFormMeasure1Change">
                <el-option
                  v-for="item in controlCategoryMeasure1Options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="管控措施分类2" prop="controlCategoryMeasure2">
              <el-select
                v-model="form.controlCategoryMeasure2"
                placeholder="请选择管控措施分类2"
                :disabled="!form.controlCategoryMeasure1">
                <el-option
                  v-for="item in controlCategoryMeasure2Options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管控措施分类3" prop="controlCategoryMeasure3">
              <el-input v-model="form.controlCategoryMeasure3" placeholder="请输入管控措施分类3" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="管控措施描述">
          <el-input v-model="form.controlMeasureDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="隐患排查内容">
          <editor v-model="form.hiddenDangerInspectionContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="隐患检查标准">
          <el-input v-model="form.hiddenDangerInspectionStandard" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMeasures, getMeasures, delMeasures, addMeasures, updateMeasures } from "@/api/system/measures";


export default {

  name: "Measures",
  data() {
    return {
      // 管控分类措施1选项
      controlCategoryMeasure1Options: [
        { label: '工程技术', value: '工程技术' },
        { label: '人员操作', value: '人员操作' },
        { label: '维护保养', value: '维护保养' },
        { label: '应急措施', value: '应急措施' }
      ],
      // 管控分类措施2选项（二级级联）
      controlCategoryMeasure2Options: [],
      // 一二级分类映射关系
      categoryMap: {
        '工程技术': [
          { label: '关键设备/部件', value: '关键设备/部件' },
          { label: '安全附件', value: '安全附件' },
          { label: '工艺控制', value: '工艺控制' },
          { label: '安全仪表', value: '安全仪表' },
          { label: '其他', value: '其他' }
        ],
        '人员操作': [
          { label: '交接班', value: '交接班' },
          { label: '人员资质', value: '人员资质' },
          { label: '操作记录', value: '操作记录' },
          { label: '其他', value: '其他' }
        ],
        '维护保养': [
          { label: '动设备', value: '动设备' },
          { label: '静设备', value: '静设备' },
          { label: '其他', value: '其他' }
        ],
        '应急措施': [
          { label: '个体防护', value: '个体防护' },
          { label: '消防设备', value: '消防设备' },
          { label: '应急预案', value: '应急预案' },
          { label: '其他', value: '其他' }
        ]
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风险管理措施表格数据
      measuresList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        riskObject: null,
        riskUnit: null,
        riskEvent: null,
        controlLevel: null,
        controlMethod: null,
        controlCategoryMeasure1: null,
        controlCategoryMeasure2: null,
        controlCategoryMeasure3: null,
        controlMeasureDescription: null,
        hiddenDangerInspectionContent: null,
        hiddenDangerInspectionStandard: null
      },
      // 表单参数
      form: {
        id: null,
        riskObject: null,
        riskUnit: null,
        riskEvent: null,
        controlLevel: null,
        controlMethod: null,
        controlCategoryMeasure1: null,
        controlCategoryMeasure2: null,
        controlCategoryMeasure3: null,
        controlMeasureDescription: null,
        hiddenDangerInspectionContent: null,
        hiddenDangerInspectionStandard: null
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询风险管理措施列表 */
    getList() {
      this.loading = true;
      listMeasures(this.queryParams).then(response => {
        this.measuresList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    
    // 表单重置
    reset() {
      this.form = {
        id: null,
        riskObject: null,
        riskUnit: null,
        riskEvent: null,
        controlLevel: null,
        controlMethod: null,
        controlCategoryMeasure1: null,
        controlCategoryMeasure2: null,
        controlCategoryMeasure3: null,
        controlMeasureDescription: null,
        hiddenDangerInspectionContent: null,
        hiddenDangerInspectionStandard: null
      };
      this.controlCategoryMeasure2Options = []; // 重置二级下拉选项
      this.resetForm("form");
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        riskObject: null,
        riskUnit: null,
        riskEvent: null,
        controlLevel: null,
        controlMethod: null,
        controlCategoryMeasure1: null,
        controlCategoryMeasure2: null,
        controlCategoryMeasure3: null,
        controlMeasureDescription: null,
        hiddenDangerInspectionContent: null,
        hiddenDangerInspectionStandard: null
      };
      this.controlCategoryMeasure2Options = []; // 重置二级下拉选项
      this.getList();
    },
    
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加风险管理措施";
    },
    
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMeasures(id).then(response => {
        this.form = response.data;
        // 加载二级分类选项
        this.controlCategoryMeasure2Options = this.categoryMap[this.form.controlCategoryMeasure1] || [];
        this.open = true;
        this.title = "修改风险管理措施";
      });
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMeasures(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMeasures(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除风险管理措施编号为"' + ids + '"的数据项？').then(function() {
        return delMeasures(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/measures/export', {
        ...this.queryParams
      }, `measures_${new Date().getTime()}.xlsx`)
    },
    
    /** 查询表单中管控分类措施1变化时加载二级选项 */
    handleQueryMeasure1Change(value) {
      this.controlCategoryMeasure2Options = this.categoryMap[value] || [];
      this.queryParams.controlCategoryMeasure2 = null; // 清空二级选择
    },
    
    /** 对话框中管控分类措施1变化时加载二级选项 */
    handleFormMeasure1Change(value) {
      this.controlCategoryMeasure2Options = this.categoryMap[value] || [];
      this.form.controlCategoryMeasure2 = null; // 清空二级选择
    }
  }
};
</script>
